<?php 
error_reporting(E_ERROR);
session_start();
require_once('constants_shop.php');
require_once('functions.php');
include('classes/userClass.php');



#conect to shop database	
$shopObj = new mysqlClass();

$available_lang= $shopObj->fetch_result("SELECT * FROM languages WHERE is_default=1 LIMIT 1");
$active_lang = $available_lang[0]['id'];

	if(isset($_POST['get_order_products'])){
		$results = $shopObj->fetch_result("SELECT * FROM order_items WHERE order_id='{$_POST['get_order_products']}'");
		echo '<table id="order_prod_table" class="table table-striped table-bordered table-hover">
			<thead>
				<tr>
					<th>Cod</th>
					<th>Name</th>
					<th>Categories</th>
					<th>Properties</th>
					<th>Bar code</th>
					<th>quantity</th>
					<th>Price buc</th>
					<th>Discount</th>
					<th>Total price</th>
				</tr>
			</thead>
			<tbody>';
			foreach($results as $key=>$value){
				$products = $shopObj->fetch_one("SELECT products.product_code,products_details.name FROM products,products_details WHERE products.id='{$value['id']}' AND 
													products.id=  products_details.product_id  AND language_id='{$active_lang}'");			

				$category = $shopObj->fetch_result("SELECT category_details.name FROM category_details
												JOIN categories ON category_details.category_id= categories.id
												JOIN products_categories ON products_categories.category_id = categories.id
												WHERE products_categories.product_id='{$value['product_id']}' AND category_details.language_id='{$active_lang}' ");
				$categs=array();
				foreach ($category as $key1 => $value1) {
					$categs[]= $value1[0];
				}
				$categories =implode(',', $categs);
				if(!$value['coupon_code']){
					$discount=0;
				}else{
					$time = time();
					$discount_data = $shopObj->fetch_one("SELECT * FROM discounts WHERE coupon_code='{$value['coupon_code']}' AND start<={$time} AND end>={$time}");
					if($discount_data){
						if($discount_data['type']==0){
							$discount = $discount_data['discount']/100*$value['price'];   
						}else{
							$discount =  $discount_data['discount'];
						}
					}else{
						$discount=0;
					}
				}	
				$stock = $shopObj->fetch_one("SELECT barcode,properties FROM stocks WHERE product_id='{$value['stock_id']}'");
				$props = json_decode($stock['properties'],true);
				foreach ($props as $key_1 => $value_1) {
					$prop_det = $shopObj->fetch_one("SELECT name FROM properties_details WHERE property_id = '{$key_1}' AND language_id='{$active_lang}'");
					$prop_val_det = $shopObj->fetch_one("SELECT name FROM properties_values_details WHERE property_value_id = '{$value_1}' AND language_id='{$active_lang}'");
					$data[]=$prop_det[0]." : ".$prop_val_det[0];
				}

				$properties = implode('<br />',$data);

				$total=($value['price']-$discount)*$value['quantity'];
				echo "<tr>
						<td>{$products['product_code']}</td>
						<td>{$products['name']}</td>
						<td>{$categories}</td>
						<td>{$properties}</td>
						<td>{$stock['barcode']}</td>
						<td>{$value['quantity']}</td>
						<td>{$value['price']}</td>
						<td>{$discount}</td>
						<td>{$total}</td>
					  </tr>";
			}
		echo '</tbody>
			</table>';
		
	}
	if(isset($_POST['print_send'])){
		$params['id'] =$_POST['print_send'];
		$params['completed']=1;
	   $shopObj->update('orders',$params);
		
		$order = $shopObj->fetch_one("SELECT * FROM orders WHERE id='{$_POST['print_send']}'");


		$county = $shopObj->fetch_one("SELECT name FROM counties WHERE id='{$order['delivery_county_id']}'");
		$country = $shopObj->fetch_one("SELECT name FROM cities WHERE id='{$order['delivery_city_id']}'");

		$ticket = "<div style='width:300px'>
			
					<span style='font-weight:bold'>Destinatar</span><br />
						<span>Nume: {$order['delivery_name']}</span><br />
						<span>Judet: {$county['name']}</span><br />
						<span>Localitate: {$country['name']}</span><br />
						<span>Adresa :{$order['delivery_address']}</span><br />
					<div>";
		echo $ticket;
	}
	if(isset($_POST['print_order'])){

		$order = $shopObj->fetch_one("SELECT * FROM orders WHERE id='{$_POST['print_order']}'");


		$county = $shopObj->fetch_one("SELECT name FROM counties WHERE id='{$order['delivery_county_id']}'");
		$country = $shopObj->fetch_one("SELECT name FROM cities WHERE id='{$order['delivery_city_id']}'");

		$ticket = "<div style='width:300px'>
					
						<span>Nume: {$order['delivery_name']}</span><br />
						<span>Judet: {$county['name']}</span><br />
						<span>Localitate: {$country['name']}</span><br />
						<span>Adresa :{$order['delivery_address']}</span><br />
					<div>";
		echo "<span>Order id: #{$_POST['print_send']}</span>";
		echo $ticket;

		$results = $shopObj->fetch_result("SELECT * FROM order_items WHERE order_id='{$_POST['print_order']}'");
		
		echo '<table border="1" style="border-collapse:0" id="order_prod_table" class="table table-striped table-bordered table-hover">
			<thead>
				<tr>
					<th>Cod</th>
					<th>Name</th>
					<th>Categories</th>
					<th>Properties</th>
					<th>Bar code</th>
					<th>quantity</th>
					<th>Price buc</th>
					<th>Discount</th>
					<th>Total price</th>
				</tr>
			</thead>
			<tbody>';
			foreach($results as $key=>$value){
				$products = $shopObj->fetch_one("SELECT products.product_code,products_details.name FROM products,products_details WHERE products.id='{$value['id']}' AND 
													products.id=  products_details.product_id  AND language_id='{$active_lang}'");			

				$category = $shopObj->fetch_result("SELECT category_details.name FROM category_details
												JOIN categories ON category_details.category_id= categories.id
												JOIN products_categories ON products_categories.category_id = categories.id
												WHERE products_categories.product_id='{$value['product_id']}' AND category_details.language_id='{$active_lang}' ");
				$categs=array();
				foreach ($category as $key1 => $value1) {
					$categs[]= $value1[0];
				}
				$categories =implode(',', $categs);
				if(!$value['coupon_code']){
					$discount=0;
				}else{
					$time = time();
					$discount_data = $shopObj->fetch_one("SELECT * FROM discounts WHERE coupon_code='{$value['coupon_code']}' AND start<={$time} AND end>={$time}");
					if($discount_data){
						if($discount_data['type']==0){
							$discount = $discount_data['discount']/100*$value['price'];   
						}else{
							$discount =  $discount_data['discount'];
						}
					}else{
						$discount=0;
					}
				}	
				$stock = $shopObj->fetch_one("SELECT barcode,properties FROM stocks WHERE product_id='{$value['stock_id']}'");
				$props = json_decode($stock['properties'],true);
				foreach ($props as $key_1 => $value_1) {
					$prop_det = $shopObj->fetch_one("SELECT name FROM properties_details WHERE property_id = '{$key_1}' AND language_id='{$active_lang}'");
					$prop_val_det = $shopObj->fetch_one("SELECT name FROM properties_values_details WHERE property_value_id = '{$value_1}' AND language_id='{$active_lang}'");
					$data[]=$prop_det[0]." : ".$prop_val_det[0];
				}

				$properties = implode('<br />',$data);

				$total=($value['price']-$discount)*$value['quantity'];
				echo "<tr>
						<td>{$products['product_code']}</td>
						<td>{$products['name']}</td>
						<td>{$categories}</td>
						<td>{$properties}</td>
						<td>{$stock['barcode']}</td>
						<td>{$value['quantity']}</td>
						<td>{$value['price']}</td>
						<td>{$discount}</td>
						<td>{$total}</td>
					  </tr>";
			}
		echo '</tbody>
			</table>';
		
	}
?>